<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/20
 */

namespace app\admin\controller\v1;

use think\facade\Filesystem;
use think\Image;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("上传管理")
 * @Apidoc\Group("system")
 * @Apidoc\Sort(10)
 */
class Upload
{

    /**
     * @Apidoc\Title("单图上传")
     * @Apidoc\Author("luoaotian")
     * @Apidoc\Url("v1/upload/imageUpload")
     * @Apidoc\Method("POST")
     * @Apidoc\ParamType("formdata")
     * @Apidoc\Param("file",type="file", require=true,desc="图片文件")
     * @Apidoc\Returned("url", type="string",desc="文件地址")
     */
    public function imageUpload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        // 上传到本地服务器
        $savename = Filesystem::putFile( 'topic', $file);

        return $savename;
    }

    public function imageUploads()
    {
        // 获取表单上传文件
        $files = request()->file('image');
        try {
            validate(['image'=>'fileSize:10240|fileExt:jpg|image:200,200,jpg'])
                ->check($files);
            $savename = [];
            foreach($files as $file) {
                $savename[] = Filesystem::putFile( 'topic', $file);
            }
        } catch (\think\exception\ValidateException $e) {
            echo $e->getMessage();
        }

        return $savename;
    }

    public function thumb()
    {
        //原图存储路径
        $og_thumb = public_path().'images.jpg';
        $image = Image::open($og_thumb);
        //文件名
        $file_name = '';
        //缩略图存储路径
        $file_path = '';
        //小缩略图
        $sm_thumb = 'sm_'.$file_name;
        //中缩略图
        $mid_thumb = 'mid_'.$file_name;
        //大缩略图
        $big_thumb = 'big_'.$file_name;
        $image->thumb(500, 500)->save(public_path().$file_path.$big_thumb);
        $image->thumb(200, 200)->save(public_path().$file_path.$mid_thumb);
        $image->thumb(80, 80)->save(public_path().$file_path.$sm_thumb);

    }
}